<?php
require_once 'mysql.php';

// 配置连接参数
$config = [
    'host' => 'localhost',
    'username' => 'username',
    'password' => 'password',
    'dbname' => 'projectDB'
];

// 连接数据库
$mysql = new mysql();

$mysql->connect($config);

$table = ['fatherclass a', 'childrenclass b'];
$field = ['a.fathername', 'b.father_id', 'b.id', 'b.name'];
$where = 'a.id=b.father_id';

$result = $mysql->select($table, $field, $where);

// print_r($mysql->fetchAll());

$i = 0;
$data['success'] = false;
$data['message'] = '';
$j = 0;
while ($row = $result->fetch_assoc()) {
    if ($row['father_id'] - 1 != $i) {
        $i += 1;
        $j = 0;
    }
    $data['docs'][$i]['id'] = $row['father_id'];
    $data['docs'][$i]['name'] = urlencode($row['fathername']);
    $data['docs'][$i]['children'][$j]['id'] = $row['id'];
    $data['docs'][$i]['children'][$j]['name'] = urlencode($row['name']);
    $j += 1;
}
$data['success'] = true;
$data['message'] = '查询成功！';

echo urldecode(json_encode($data, true));
